package proxynew;

import java.util.Enumeration;
import java.util.Hashtable;

import net.jxta.protocol.PipeAdvertisement;

public class Pipes {
	private Hashtable<String, PipeAdvertisement> pipeMap;
	private static Pipes instance = null; 
	
	private Pipes() {
		pipeMap = new Hashtable<String, PipeAdvertisement>();
	}
	
	public static Pipes getInstance() {
		if (instance == null) {
			instance = new Pipes();
		}
		return instance;
	}
	
	public void addPipe(String pipe, PipeAdvertisement advertisement) {
		synchronized (pipeMap) {
			pipeMap.put(pipe, advertisement);
		}
	}
	
	public Enumeration<String> getPipes() {
		synchronized (pipeMap) {
			return pipeMap.keys();
		}
	}
	
	public PipeAdvertisement getAdvertisement(String pipe) {
		synchronized (pipeMap) {
			return pipeMap.get(pipe);	
		}
	}
	
	public void removePipe(String pipe) {
		synchronized (pipeMap) {
			pipeMap.remove(pipe);
			System.out.println("Pipes em cache:\n" + Pipes.getInstance());
		}
	}
	
	public String toString() {
		if (pipeMap != null) {
			StringBuffer sb = new StringBuffer();
			Enumeration<String> keys = pipeMap.keys();
			while (keys.hasMoreElements()) {
				String pipeId = keys.nextElement();
				sb.append("pipeID =[" + pipeId + "]\n");	
			}
			return sb.toString();
		} else {
			return null;
		}
	}
}
